package com.example.coinhelper.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.coinhelper.Bean.Details;
import com.example.coinhelper.R;

import java.util.Calendar;
import java.util.List;

public class DetailsAdapter extends BaseAdapter {
    private Context context;
    private List<Details> detailsList;
    LayoutInflater inflater;

    public DetailsAdapter(Context context, List<Details> detailsList) {
        this.context = context;
        this.detailsList = detailsList;
        inflater = LayoutInflater.from(context);
        Calendar calendar = Calendar.getInstance();
    }

    public DetailsAdapter() {

    }

    @Override
    public int getCount() {
        return detailsList.size();
    }

    @Override
    public Object getItem(int position) {
        return detailsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_details, parent, false);
            viewHolder = new ViewHolder(convertView);
            if(viewHolder==null){
                System.out.println("viewHolder为空");
                return null;
            }
            viewHolder.dateTextView = convertView.findViewById(R.id.item_details_tv_time);
            viewHolder.operationTextView = convertView.findViewById(R.id.item_details_tv_title);
            viewHolder.moneyTextView = convertView.findViewById(R.id.item_details_tv_money);
            viewHolder.iconImageView = convertView.findViewById(R.id.item_details_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Details details = detailsList.get(position);
        viewHolder.otherTextView.setText(details.getOther());
        viewHolder.dateTextView.setText(details.getDate());
        viewHolder.operationTextView.setText(details.getOperation());
        viewHolder.moneyTextView.setText("￥"+String.valueOf(details.getMoney()));
        viewHolder.iconImageView.setImageResource(details.getIcon());
        return convertView;
    }

    public void updateDetailsList(List<Details> updatedDetails) {
        this.detailsList = updatedDetails;
    }

    private class ViewHolder {
        TextView otherTextView;
        TextView dateTextView;
        TextView operationTextView;
        TextView moneyTextView;
        ImageView iconImageView;
        public ViewHolder(@NonNull View view){
            iconImageView = view.findViewById(R.id.item_details_iv);
            operationTextView = view.findViewById(R.id.item_details_tv_title);
            otherTextView = view.findViewById(R.id.item_details_tv_beizhu);
            dateTextView = view.findViewById(R.id.item_details_tv_time);
            moneyTextView = view.findViewById(R.id.item_details_tv_money);
        }
    }

}
